(function($) {
	var datagrid = '#dg_user', toolbar = '#toolbars_user', add = '#add_user', del = '#delete_user', update = '#update_user', reload = '#reload_user',
		form = '#form_user', submit = '#submit', closeWin = '#closeWin', search = '#search_user',
		lockAccount = $('#lockAccount'), unLockAccount = $('#unLockAccount'), resetPassword = $('#resetPassword');
	
	var UserObj = function() {
		var self = this ;
		this.dg = null ;
		this.url_form = window.app.domain.URL_ADMIN_UI + '/pages/user/form.html' ;
		this.url_list = window.app.domain.URL_API_MANAGER_REST + '/user/list' ;
		this.url_add = window.app.domain.URL_API_MANAGER_REST + '/user/add' ;
		this.url_update = window.app.domain.URL_API_MANAGER_REST + '/user/update' ;
		this.url_delete = window.app.domain.URL_API_MANAGER_REST + '/user/delete' ;
		this.url_get = window.app.domain.URL_API_MANAGER_REST + '/user/get' ;
		this.url_updateAccountStatus = window.app.domain.URL_API_MANAGER_REST + '/user/updateAccountStatus' ;
		this.url_resetPassword = window.app.domain.URL_API_MANAGER_REST + '/user/resetPassword' ;
		
		this.initDatagrid() ;
		this.initBindBtnEvent();
		
		//搜索
		$(search).searchbox({
			prompt: '请输入关键字',
			menu:'#search_mm_user',
			searcher:function(value,name){
				var params = {} ;
				params[name] = value ;
				self.dg.datagrid('load', params);
		    }
		});
	};
	
	UserObj.prototype.initDatagrid = function() {
		var self = this ;
		this.dg = $(datagrid).datagrid({
			url: self.url_list, 
			method: 'GET', nowrap: true, striped: true, singleSelect: false,
			checkbox: true, pagination: true, pageSize: 30, pageList: [30,40,50,100,200,500,1000],
			autoRowHeight: true, fit: true, border: false, rownumbers: true, rownumberWidth: 40, toolbar: toolbar, 
			remoteSort: true, sortName: 'id', sortOrder: 'desc', queryParams: {},
			frozenColumns: [[
			           {title: 'ID', field: 'id', hidden: true},
			           {field: 'ck', checkbox:true},
			           {title: '用户名', field: 'username', width: 100},
			]],
			columns: [[
			           {title: '真实姓名', field: 'realname', width: 100},
			           {title: '性别', field: 'gender', width: 60, align: 'center', sortable: true, formatter: function(value, row, index) {
			        	   if(value == 1) return '男';
			        	   else if(value == 2) return '女';
			        	   else return '未知';
			           }},
			           {title: '手机号码', field: 'mobile', width: 110},
			           {title: '电子邮箱', field: 'email', width: 200},
			           {title: '状态', field: 'status', width: 80, align: 'center', sortable: true, formatter: function(value, row, index) {
			        	   if(value == 1) return '<span class="label label-success">激活</span>';
			        	   else return '<span class="label label-danger">禁用</span>';
			           }},
			           {title: '用户类型', field: 'type', width: 150, align: 'center', sortable: true, formatter: function(value, row, index) {
			        	   if(value == 1) return '<span>超级管理员</span>';
			        	   else if(value == 2) return '<span>普通管理员</span>';
			        	   else return '<span>普通用户</span>';
			           }},
			           {title: '创建时间', field: 'createTime', width: 130, align: 'center', sortable: true, formatter: function(value, row, index) {
			        	   return value != null && value != '' ? $.date.format(value, 'yyyy-MM-dd HH:mm:ss') : '';
			           }},
			           {title: '更新时间', field: 'updateTime', width: 130, align: 'center', sortable: true, formatter: function(value, row, index) {
			        	   return value != null && value != '' ? $.date.format(value, 'yyyy-MM-dd HH:mm:ss') : '';
			           }},
			]],
			onLoadSuccess: function(data) {
			}
		});
	};
	
	UserObj.prototype.initBindBtnEvent = function() {
		var self = this ;
		$(add).on('click', function() { self.add() });
		$(del).on('click', function() { self.del() });
		$(update).on('click', function() { self.update() });
		$(reload).on('click', function() { self.reload() });
		$(lockAccount).on('click', function() { self.updateAccountStatus(1) });
		$(unLockAccount).on('click', function() { self.updateAccountStatus(2) });
		$(resetPassword).on('click', function() { self.resetPassword() });
	};
	
	UserObj.prototype.add = function() {
		var self = this ;
		window.app.easyui.addWindow({title: '添加', href: self.url_form, width: 600, height: 320,
			onLoad: function() {
				$(submit).on('click', function() {
					self.submitForm(self.url_add);
				});
			}
		});
	};
	
	UserObj.prototype.update = function() {
		var self = this ;
		var selections = window.app.easyui.getSelections(datagrid) ;
		if(selections) {
			if(selections.length == 1) {
				window.app.easyui.addWindow({title: '修改', href: self.url_form, width: 600, height: 320,
					onLoad: function() {
						
						$.get(self.url_get + '/' + selections[0], function(result) {
							$(form).form('load', result);
						})
						
						$(submit).on('click', function() {
							self.submitForm(self.url_update);
						});
					}
				});
			} else {
				alertify.error('只能选择一条记录！');
			}
		} else {
			alertify.error('请选择记录！');
		}
		
	};
	
	UserObj.prototype.updateAccountStatus = function(status) {
		var self = this ;
		var selections = window.app.easyui.getSelections(datagrid) ;
		if(selections) {
			$.messager.confirm((status == 1 ? '解锁帐号' : '锁定帐号'), '确定要'+(status == 1 ? ' <span style="color:green">解锁</span> 帐号' : ' <span style="color:red">锁定</span> 帐号')+'？', function(r) {
				if(r) {
					$.ajax({
						type: 'POST',
						url: self.url_updateAccountStatus,
						data: {'ids': selections.join(','), 'status': status},
						beforeSend: function() {
							$.easyui.loading();
						},
						success: function(result) {
							try {
								if(result.status) {
									self.reload(); alertify.success(result.message);
								} else {
									alertify.warning(result.message);
								}
							} catch(e) {
								$.easyui.loaded(); console.error(e) ;
							}
						},
						complete: function(xhr, textStatus) {
							$.easyui.loaded();
						}
					});
				}
			});
		} else {
			alertify.error('至少选择一条记录');
		}
	};
	
	UserObj.prototype.resetPassword = function(status) {
		var self = this ;
		var selections = window.app.easyui.getSelections(datagrid) ;
		if(selections) {
			$.messager.confirm('重置密码', '确定要重置密码？', function(r) {
				if(r) {
					$.ajax({
						type: 'POST',
						url: self.url_resetPassword,
						data: {'ids': selections.join(',')},
						beforeSend: function() {
							$.easyui.loading();
						},
						success: function(result) {
							try {
								if(result.status) {
									self.reload(); alertify.success(result.message);
								} else {
									alertify.warning(result.message);
								}
							} catch(e) {
								$.easyui.loaded(); console.error(e) ;
							}
						},
						complete: function(xhr, textStatus) {
							$.easyui.loaded();
						}
					});
				}
			});
		} else {
			alertify.error('至少选择一条记录');
		}
	};
	
	UserObj.prototype.del = function() {
		var self = this ;
		var selections = window.app.easyui.getSelections(datagrid) ;
		if(selections) {
			$.messager.confirm('删除确认', '确定要删除？', function(r) {
				if(r) {
					$.ajax({
						type: 'GET',
						url: self.url_delete + '/' + selections.join(','),
						beforeSend: function() {
							$.easyui.loading();
						},
						success: function(result) {
							try {
								if(result.status) {
									self.reload(); alertify.success(result.message);
								} else {
									alertify.warning(result.message);
								}
							} catch(e) {
								$.easyui.loaded(); console.error(e) ;
							}
						},
						complete: function(xhr, textStatus) {
							$.easyui.loaded();
						}
					});
				}
			});
		} else {
			alertify.error('至少选择一条记录');
		}
	};
	
	UserObj.prototype.get = function(id) {
		var self = this ;
		$.ajax({
			type: 'GET',
			url: self.url_get + id,
			beforeSend: function() {
				$.easyui.loading();
			},
			success: function(result) {
				try {
					return result ;
				} catch(e) {
					$.easyui.loaded(); console.error(e) ;
				}
			},
			complete: function(xhr, textStatus) {
				$.easyui.loaded();
			}
		});
	};
	
	UserObj.prototype.submitForm = function(url) {
		var self = this ;
		var checkform = $(form).form('validate');
		if(checkform) {
			$.ajax({
				type: 'POST',
				url: url,
				data: window.app.util.getData(form),
				beforeSend: function() {
					$.easyui.loading();
				},
				success: function(result) {
					try {
						if(result.status) {
							self.reload(); window.app.easyui.closeCurrentWindow(); alertify.success(result.message);
						} else {
							alertify.warning(result.message);
						}
					} catch(e) {
						$.easyui.loaded(); console.error(e) ;
					}
				},
				complete: function(xhr, textStatus) {
					$.easyui.loaded();
				}
			});
		}
	};
	
	UserObj.prototype.reload = function() {
		this.dg.datagrid('reload'); this.dg.datagrid('clearSelections');this.dg.datagrid('clearChecked');
	};
	
	var user = new UserObj() ;
})(jQuery);
	
